import java.util.List;


public class PokerAI {

	private PokerGame game = new PokerGame();

	public int play(Player dealer) {

		List<Card> dealerHand = dealer.getHand();

		int dealerValue = game.evaluateHand(dealer).handValue();

		switch (dealerValue) {
		case 4: {
			if (game.isFour(dealerHand)) {
				// if the non-matching card is less than 10, swap it out for a higher card
				dealer.removeCard(dealerHand.get(0));
				return 1;
			}
		}
		case 3: {
			if (game.isThree(dealerHand)) {
				if (game.isFullHouse(dealerHand) == false) {
					// swap out the lower card
					if (dealerHand.get(0).compareTo(dealerHand.get(1)) > 0) {
						dealer.removeCard(dealerHand.get(1));
					}

					else {

						dealer.removeCard(dealerHand.get(0));
					}

					return 1;
				}
			}

			break;
		}
		case 2: {
			if (game.isTwoPair(dealerHand)) {
				// if the non-matching card is less than 10, swap it out for a higher card
				dealer.removeCard(dealerHand.get(0));
				return 1;
			}
		}
		case 1: {
			if (game.isPair(dealerHand)) {
				
				// Drop the lowest card not in the pair
				dealer.removeCard(game.getLowest(dealerHand.subList(0, 3)));
				// Remove that card from consideration
				dealerHand.remove(game.getLowest(dealerHand.subList(0, 3)));
				// drop the lowest remaining card not in pair
				dealer.removeCard(game.getLowest(dealerHand.subList(0, 2)));
				
				return 2;
			}
		}
		case 0: {
			
			// check for four of the same suit
			if (game.getCountOfSameSuit(dealerHand) == 4) {
				// drop card of other suit
				dealer.removeCard(dealerHand.get(0));
				
				return 1;
			}
			// check for four in a row
			if (game.hasStraightOfFour(dealerHand)) {
				// drop the card that isn't part of the possible straight
				dealer.removeCard(dealerHand.get(0));
				
				return 1;
			}
			
			// check for three of the same suit
			if (game.getCountOfSameSuit(dealerHand) == 3) {
				// drop cards of other suits
				dealer.removeCard(dealerHand.get(1));
				dealer.removeCard(dealerHand.get(0));
				
				return 2;
			}
			
			// fine, just keep the highest card
			else {
				
				Card highest = game.getHighCard(dealerHand);
				dealerHand.remove(highest);
				
				for (Card card : dealerHand) {
					dealer.removeCard(card);
				}
				
				return 4;
			}
		}
		}

		return 0;
	}

}
